using Game.Main.Model;
using UnityEngine;

namespace Game.Component
{
	public class JewelryHolderItem : BaseItem
	{
		private Transform head;

		private Transform face;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			head = model.Find("Head");
			InitComponentTrigger(head.gameObject, ItemComponentTrigger.Type.Head, null, null, OnComponentAttachItemTrigger);
			face = head.Find("Face");
			if (face != null)
			{
				InitComponentTrigger(face.gameObject, ItemComponentTrigger.Type.Face, null, null, OnComponentAttachItemTrigger);
			}
		}

		private void InitComponentTrigger(GameObject obj, ItemComponentTrigger.Type type, ItemComponentTrigger.EnterCollision onEnter, ItemComponentTrigger.ExitCollision onExit, ItemComponentTrigger.AttachItem onAttachItem)
		{
			obj.layer = 12;
			ItemComponentTrigger itemComponentTrigger = obj.AddComponent<ItemComponentTrigger>();
			itemComponentTrigger.Init(this, type, onEnter, onExit, onAttachItem);
		}

		public Transform OnComponentAttachItemTrigger(BaseItem item, ItemComponentTrigger trigger)
		{
			if (trigger.type == ItemComponentTrigger.Type.Head)
			{
				if (AttachItem(item, DepthType.Front, AttachType.PutHead, false))
				{
					return head;
				}
			}
			else if (trigger.type == ItemComponentTrigger.Type.Face && AttachItem(item, DepthType.Front, AttachType.PutFace, false))
			{
				return head;
			}
			return null;
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (item.parentItem != null)
			{
				return false;
			}
			switch (attachType)
			{
			case AttachType.PutHead:
				AttachItemToHead(item, updatePosition);
				break;
			case AttachType.PutFace:
				AttachItemToFace(item, updatePosition);
				break;
			}
			return base.AttachItem(item, depthType, attachType, updatePosition);
		}

		private void AttachItemToHead(BaseItem item, bool updatePosition)
		{
			item.SetParent(head.transform);
			if (updatePosition)
			{
				item.SetLocalPosition(item.itemData.putHeadX, item.itemData.putHeadY);
			}
		}

		private void AttachItemToFace(BaseItem item, bool updatePosition)
		{
			item.SetParent(head.transform);
			if (updatePosition)
			{
				item.SetLocalPosition(item.itemData.putFaceX, item.itemData.putFaceY);
			}
		}

		public override bool CanPutHead()
		{
			if (head == null)
			{
				return false;
			}
			return true;
		}

		public override bool CanPutFace()
		{
			if (face == null)
			{
				return false;
			}
			return true;
		}
	}
}
